import pyautogui as pi
import cv2 as cv
import numpy as np
import time
password = '123456'

'''
serch img in default pic by screenshot
temfile : path to img what you wanna search
debug : boolean, True for debug , False fo no debug
'''
#def find_img(tempFile, debug =False):
def find_img(tempFile, debug =True):
    time.sleep(0.75)
    pi.screenshot('main.png')
    gray = cv.imread('main.png', 0)
    img_temp = cv.imread(tempFile, 0)
    (w, h) = img_temp.shape[::-1]
    res = cv.matchTemplate(gray, img_temp, cv.TM_SQDIFF)
    (min_val, max_val, min_loc, max_loc) = cv.minMaxLoc(res)
    top = min_loc[0]
    left = min_loc[1]
    x = [
        top,
        left,
        w,
        h]
    top_left = min_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    (x, y) = pi.center(x)
    if debug:
        img = cv.imread('main.png', 1)
        cv.rectangle(img, top_left, bottom_right, (0, 0, 255), 2)
        img = cv.resize(img, None,fx=0.7,fy=0.7, interpolation = cv.INTER_NEAREST)
        cv.imshow('process', img)
        cv.waitKey(0)
        cv.destroyAllWindows()
    return (x, y)

'''
shangwu walkthrough
'''
def click_shangwu():
    (x, y) = find_img('./image/shangwu.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/shangchuan.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/pdf_file.png')
    pi.leftClick(x, y)
    pi.press('down')
    pi.press('enter')
    (x, y) = find_img('./image/qianzhang.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/danyeqianzhang.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/cainput.png')
    pi.leftClick(x, y)
    pi.write(password)
    (x, y) = find_img('./image/confirm.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/clickqianzhang.png')
    pi.moveTo(x, y)
    pi.leftClick(x, y)
    time.sleep(3.5)
    (x, y) = find_img('./image/realinputca.png')
    pi.leftClick(x, y)
    pi.write(password)
    (x, y) = find_img('./image/confirm1.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/save.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)
    (x, y) = find_img('./image/close.png')
    pi.click(x, y)
    (x, y) = find_img('./image/querengwancheng.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)

'''
fujian click walkthrough
'''
def fujian():
    (x, y) = find_img('./image/fujian.png')
    pi.click(x, y)
    (x, y) = find_img('./image/querengwancheng.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)

'''
jishu click  walkthrough
'''
def click_jishu():
    (x, y) = find_img('./image/jishu.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/shangchuan.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/pdf_file.png')
    pi.leftClick(x, y)
    pi.press('down')
    pi.press('enter')
    (x, y) = find_img('./image/qianzhang.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/danyeqianzhang.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/cainput.png')
    pi.leftClick(x, y)
    pi.write(password)
    (x, y) = find_img('./image/confirm.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/clickqianzhang.png')
    pi.moveTo(x, y)
    pi.leftClick(x, y)
    time.sleep(3)
    (x, y) = find_img('./image/save.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)
    (x, y) = find_img('./image/close.png')
    pi.click(x, y)
    (x, y) = find_img('./image/querengwancheng.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)

'''
take the price walkthrough 
'''
def click_jiage():
    (x, y) = find_img('./image/jiage.png')
    pi.leftClick(x, y)
    pi.alert('\t暂停\n完成开标一览表后点击完成,继续脚本')
    (x, y) = find_img('./image/zhuwenjian.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/shangchuan.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/pdf_file.png')
    pi.leftClick(x, y)
    pi.press('down')
    pi.press('enter')
    (x, y) = find_img('./image/hebing.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)
    (x, y) = find_img('./image/qianzhang.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/danyeqianzhang.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/cainput.png')
    pi.leftClick(x, y)
    pi.write(password)
    (x, y) = find_img('./image/confirm.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/clickqianzhang.png')
    pi.moveTo(x, y)
    pi.leftClick(x, y)
    time.sleep(3)
    (x, y) = find_img('./image/save.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)
    (x, y) = find_img('./image/close.png')
    pi.click(x, y)
    (x, y) = find_img('./image/querengwancheng.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)

'''
tiaokuan click walkthrough
'''
def click_tiaokuan():
    (x, y) = find_img('./image/guanlian.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/guanlianopen.png')
    pi.leftClick(x, y)
    pi.alert('\t暂停\n完成关联后点击确认,继续脚本')
    (x, y) = find_img('./image/querengwancheng.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirm0.png')
    pi.click(x, y)

'''
toubiao walkthrough
'''
def click_toubiao():
    (x, y) = find_img('./image/shengcheng.png')
    pi.click(x, y)
    (x, y) = find_img('./image/covertfile.png')
    pi.click(x, y)
    (x, y) = find_img('./image/cainput.png')
    pi.leftClick(x, y)
    pi.write(password)
    (x, y) = find_img('./image/confirm.png')
    pi.leftClick(x, y)
    (x, y) = find_img('./image/choosefile.png')
    pi.click(x, y)
    (x, y) = find_img('./image/confirmend.png')
    pi.click(x, y)

if __name__ == '__main__':
    pi.alert('开始')
    click_shangwu()
    fujian()
    click_jishu()
    fujian()
    click_jiage()
    fujian()
    click_tiaokuan()
    click_toubiao()
